package hejie.admin.action;

import hejie.admin.service.LoanTypeService;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.opensymphony.xwork2.ActionSupport;

import publics.model.LoanType;
import publics.util.MyUtils;
import publics.util.Page;

public class LoanTypeAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	LoanTypeService loanTypeService;  //spring注入进来的 loanTypeService
	private boolean success;   //此参数为必须的， extjs根据这个参数的值判断是否成功执行操作
	private LoanType ltype;
	private Page page;   //这是分页查询用的辅助类，用此类可以解决分页查询中要返回多个参数的问题，推荐使用
	private Integer id;
	private String conditions;//查询条件（即搜索页面上文本框中输入的值），多个条件用空格分开
	private int start;   //分页查询的 要查询数据的开始号 ，和offset值相同
	private int limit;  // 每页多少个记录

	public LoanTypeService getLoanTypeService() {
		return loanTypeService;
	}
	@Resource(name="loanTypeService")
	public void setLoanTypeService(LoanTypeService loanTypeService) {
		this.loanTypeService = loanTypeService;
		System.out.println("LoanTypeServiceImlp inject in LoanTypeAction!");
	}

	/**
	 * 添加贷款种类
	 * @return
	 */
	public String saveLoanType() {
		System.out.println("已经进入 Action 的 saveLoanType方法！");//测试用，可去掉
		id = (Integer) loanTypeService.saveLoanType(ltype);
		if (id != null) {
			success = true;
		}else{
			success = false;  //失败返回
		}
		System.out.println("准备返回，success="+success);
		return SUCCESS;
	}

	/**
	 * 根据条件分页查找贷款种类
	 * @return
	 */
	public String findAllLoanType() {  
		List<String> conditionList = new ArrayList<String>();   //模糊查询使用
		MyUtils.addToCollection(conditionList, MyUtils.split(conditions, " ,"));//模糊查询使用
		page = new Page(); // 实例化分页对象
		page.setConditions(conditionList);// 设置查询条件
		page.setStart(start);
		page.setLimit(limit = (limit == 0) ? 500 : limit);
		page = loanTypeService.findByPage(page);  //查询 
		if ((null != page.getRoot() && page.getTotalProperty() > 0 )||(null == page.getRoot() && page.getTotalProperty() == 0 )) {
			page.setSuccess(true);
		}else{
			page.setSuccess(false);//查找失败
		}
		return SUCCESS;
	}

	/**
	 * 删除贷款种类
	 * @return
	 */
	public String deleteLoanType() {
		if(Integer.valueOf(id) < 1){
			System.out.println("修改失败！贷款种类id为="+ltype.getId());
			success = false;
		}else{
		System.out.println("loanTypeAction delete id="+id);	
		success = loanTypeService.deleteLoanType(Integer.valueOf(id));
		}
		return SUCCESS;
	}
	
	/**
	 * 修改贷款种类指定字段的值
	 * @return
	 * @throws Exception
	 */
	public String updateLoanType() throws Exception {
		
		if(Integer.valueOf(ltype.getId()) < 1){
			System.out.println("修改失败！ 贷款种类id为="+ltype.getId());
			success = false;
		}else{
			success = loanTypeService.updateLoanType(ltype);
		}
		return SUCCESS;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public LoanType getLoanType() {
		return ltype;
	}

	public void setLoanType(LoanType ltype) {
		this.ltype = ltype;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String getConditions() {
		return conditions;
	}

	public void setConditions(String conditions) {
		this.conditions = conditions;
	}

}
